<template>
  <!-- 智慧贷款的用户录入信息 -->
  <div class="userLoans-box">
    <!-- 公司名称 -->
    <h3 class="loans-title">{{companyName}}</h3>
    <!-- 资格条件 -->
    <div class="loans-box">
        <h3>资格条件</h3>
        <div class="cond_tit">
            <span class="cond_span">序号</span>
            <p>资格</p>
        </div>
        <div class="cond_list" v-for="(cond,index) in qualifData" :key="index">
            <span class="cond_span">{{index+1}}</span>
            <p>{{cond.name}}</p>
        </div>
    </div>
    <!-- 押品信息 -->
    <div class="loans-box">
        <h3>押品信息</h3>
        <div class="collateral-box" >
            <h3>
                <p>类型</p>
                <p>名称</p>
                <p>单位</p>
                <p>估计(万元)</p>
                <p>处所</p>
                <p>说明</p>
            </h3>
            <ul>
                <li v-for="(item,index) in collateralData" :key="index">
                    <p>{{getType(item.type)}}</p>
                    <p>{{item.name}}</p>
                    <p>{{item.unit || '--'}}</p>
                    <p>{{(item.price/10000) | sumFilters}}</p>
                    <p>{{item.address}}</p>
                    <p>{{item.description}}</p>
                </li>
            </ul>
        </div>
    </div>
    <!-- 运算结果 -->
    <div class="loans-box">
        <h3>运算结果</h3>
        <el-table :data="resultList" style="width: 100%">
            <el-table-column width="100" align="center" label="产品名称">
                <template slot-scope="scope">
                    <p>{{scope.row.name}}</p>
                </template>
            </el-table-column>
            <el-table-column label="预计可贷(万)" align="center" class="bright">
                <template slot-scope="scope">
                    <p :class="getOptimalLimit(scope.row.actualAmount)">{{getRoDecimal(scope.row.actualAmount/10000)}}</p>
                </template>
            </el-table-column>
            <el-table-column label="名义利率" align="center">
                <template slot-scope="scope">
                    <p :class="getOptimalint(scope.row.nominalRate)">{{scope.row.nominalRate}}%</p>
                </template>
            </el-table-column>
            <el-table-column label="年利息(万)" align="center">
                <template slot-scope="scope">
                    <p :class="getOptimalinterest(scope.row.expectedInterest)">{{getRoDecimal(scope.row.expectedInterest/10000)}}</p>
                </template>
            </el-table-column>
            <el-table-column label="贷款年限" align="center">
                <template slot-scope="scope">
                    <p :class="getTerm(scope.row.term)">{{getRoDecimal(scope.row.term)}}</p>
                </template>
            </el-table-column>
            <el-table-column label="办结天数" align="center">
                <template slot-scope="scope">
                    <p :class="getOptimalDay(scope.row.closingDays)">{{scope.row.closingDays}}</p>
                </template>
            </el-table-column>
            <el-table-column label="大约收费(万)" align="center">
                <template slot-scope="scope">
                    <p :class="getExpectedCost(scope.row.expectedCost)">{{scope.row.expectedCost/10000}}</p>
                </template>
            </el-table-column>
        </el-table>
    </div>
  </div>
</template>

<script>
import {getInfoLoanUserApi,getConditionIdsloanUserApi,getCollateralListApi,getLoanUserFindLoanApi} from "@/wss/newApi_3-24/tool/loan.js"
import { getLoanConditionListApi } from "@/wss/newApi_3-24/tool/loanCondition.js"
import {getCollateralTypeListApi} from "@/wss/newApi_3-24/tool/collateralRate.js"
import {toDecimal} from "@/wss/utils/wss.js"
export default {
    name: 'userLoans',
    props:{
        // 当前订单信息
        projectData: {
            type: Object,
            default: () => {
                return {};
            },
        },
    },
    data(){
        return{
            loanUserId: '',//智慧贷款id
            companyName:'',//公司名称
            allConds: [],//所有资格条件集合
            qualifData:[],//资格条件的集合

            collateralData: [],//所有押品信息
            pawnList: [],// 获取抵押物列表
            resultList:[], // 运算结果
        }
    },
    mounted(){
        console.log(this.projectData,'当前订单信息当前订单信息');
        // 获取智慧贷款id
        this.loanUserId = this.projectData.loanUserId
        if(this.loanUserId){
            this.getInfoLoanUser() // 查询贷款信息
            this.getLoanConditionList() // 获取所有资格条件
            this.getCollateralTypeList() // 获取抵押物列表
            this.getLoanUserFindLoan() // 查询用户获取的结果列表
        }
    },
    watch:{
        'projectData.loanUserId':{
            handler(){
                this.getInfoLoanUser() // 查询贷款信息
                this.getLoanConditionList() // 获取所有资格条件
                this.getCollateralTypeList() // 获取抵押物列表
                this.getLoanUserFindLoan() // 查询用户获取的结果列表
            },
        },
    },
    methods:{
        // -----------------请求-----------------
        // 查询贷款信息
        async getInfoLoanUser(){
            let id = this.loanUserId
            let result = await getInfoLoanUserApi({id})
            if(result.code==200){
                this.companyName = result.data.companyName
            }
        },

        // 获取所有资格条件
        async getLoanConditionList(){
            let result = await getLoanConditionListApi()
            if(result.code==200){
                this.allConds = result.rows
                this.getConditionIdsloanUser() // 查询用户勾选的条件
            }
        },

        // 查询用户勾选的条件
        async getConditionIdsloanUser(){
            let loanUserId = this.loanUserId
            let result = await getConditionIdsloanUserApi({loanUserId})
            if(result.code==200){
                let list = result.data.split(',')
                list.forEach(item => {
                    let obj = this.allConds.find(f=>item==f.id)
                    this.qualifData.push(obj)
                });
            }
        },

        // 获取抵押物列表
        async getCollateralTypeList(){
            let result = await getCollateralTypeListApi()
            if(result.code == 200){
                this.pawnList = result.rows
                this.getCollateralList() // 查询用户填写的抵押物
            }
        },

        // 查询用户填写的抵押物
        async getCollateralList(){
            let loan_user_id = this.loanUserId
            let result = await getCollateralListApi({loan_user_id})
            if(result.code == 200){
                let list = result.rows
                list.forEach(item=>{
                    let obj = this.pawnList.find(f=>item.typeId==f.id)
                    item.name = obj.name //名称
                    item.unit = obj.unit //单位
                    item.type = obj.type //类型
                })
                this.collateralData = list
            }
        },

        // 查询用户获取的结果列表
        async getLoanUserFindLoan(){
            let loanUserId = this.loanUserId
            let result = await getLoanUserFindLoanApi({loanUserId})
            if(result.code == 200){
                this.resultList = result.rows
            }
        },

        // -----------------请求-----------------end

        //获取类型
        getType(type){
            // 1实物，2信用类型，3金融资产 4特殊许可
            if(type == '1'){
                return '实物资产'
            }else if(type == '2'){
                return '信用类型'
            }else if(type == '3'){
                return '金融资产'
            }else{
                return '特殊许可'
            }
        },

        // 获取关系 
        getCalculation(item){
            // 1相加/累加，2max/最大，3min/最小
            if(item.calculation == '1'){
                return '累加'
            }else if(item.calculation == '2'){
                return '最大'
            }else{
                return '最小'
            }
        },

        // ----------------运算结果----------------
        // 自定义索引
        indexMethod(inedx){
            if (inedx >= 0 && inedx <= 25) {
                return String.fromCharCode(65 + inedx);
            } 
        },

        //获取最优贷款额
        getOptimalLimit(actualAmount){
            let max = 0
            let num = Number(actualAmount)
            for (let i = 0; i < this.resultList.length; i++) {
                if(this.resultList[i].actualAmount>max){
                    max = this.resultList[i].actualAmount
                }
            }
            if(num==max){
                return 'isLight'
            }
        },

        //获取最优实际利率
        getOptimalint(nominalRate){
            let arr = []
            let num = Number(nominalRate)
            for (let i = 0; i < this.resultList.length; i++) {
                arr.push(this.resultList[i].nominalRate)
            }
            let min = Math.min(...arr)
            if(num==min){
                return 'isLight'
            }
        },

        //获取最优预估利息
        getOptimalinterest(expectedInterest){
            let arr = []
            let num = Number(expectedInterest)
            for (let i = 0; i < this.resultList.length; i++) {
                arr.push(this.resultList[i].expectedInterest)
            }
            let min = Math.min(...arr)
            if(num==min){
                return 'isLight'
            }
        },

        // 获取最优的贷款年限
        getTerm(term){
            let arr = []
            let num = Number(term)
            for (let i = 0; i < this.resultList.length; i++) {
                arr.push(this.resultList[i].term)
            }
            let min = Math.max(...arr)
            if(num==min){
                return 'isLight'
            }
        },

        //获取最优办结天数
        getOptimalDay(closingDays){
            let arr = []
            let num = Number(closingDays)
            for (let i = 0; i < this.resultList.length; i++) {
                arr.push(this.resultList[i].closingDays)
            }
            let min = Math.min(...arr)
            if(num==min){
                return 'isLight'
            }
        },

        //获取最优大约收费
        getExpectedCost(expectedCost){
            let arr = []
            let num = Number(expectedCost)
            for (let i = 0; i < this.resultList.length; i++) {
                arr.push(this.resultList[i].expectedCost)
            }
            let min = Math.min(...arr)
            if(num==min){
                return 'isLight'
            }
        },
        
        // 获取两位小数
        getRoDecimal(num){
            return toDecimal(num)
        },
        
        // 获取两位小数
        getToDecimal(x) {
            if (x < 0.01) {
                return x
            } else {
                var f = parseFloat(x);
                if (isNaN(f)) {
                    return;
                }
                f = Math.round(x * 100) / 100;
                return f;
            }
        },
        // ----------------运算结果----------------end
    },
}
</script>

<style scoped lang="scss">
h3,p,li,ul{
    margin: 0;
    padding: 0;
    list-style: none;
}
.userLoans-box{
    padding: 0 30px;
    .loans-title{
        font-size: 18px;
        font-weight: bold;
        margin-bottom: 15px;
        padding-bottom: 15px;
        border-bottom: 1px solid #efefef;
    }
}

.loans-box{
    border-bottom: 1px solid #efefef;
    margin-bottom: 30px;
    h3{
        color: #2E3F5E;
        font-size: 16px;
        margin-bottom: 15px;
    }
    ul{
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        font-size: 15px;
        li{
            width: 33%;
            color: #999;
            line-height: 30px;
            margin-bottom: 10px;
            p{
                color: #333;
            }
        }
    }

    // 资格
    .cond_tit,
    .cond_list{
        display: flex;
        font-size: 13px;
        color: #999;
        line-height: 46px;
        font-weight: bold;
        >p,>span{
            width: 90%;
            overflow:hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            -o-text-overflow:ellipsis;
        }
        .cond_span{
            width: 10%;
        }
    }
    .cond_list{
        font-size: 15px;
        font-weight: 400;
        color: #333;
        border-top: 1px solid #efefef;
    }
}

/* 押品押率 */
.collateral-box{
    h3{
        display: flex;
        font-size: 13px;
        font-weight: 600;
        color: #999;
        border-bottom: 1px solid #ebeef5;
        padding-bottom: 15px;
        margin-top: 25px;
        margin-bottom: 12px;
        p{
            width: 17%;
        }
    }
    ul{
        display: block;
        li{
            width: 100%;
            display: flex;
            font-size: 14px;
            font-weight: 400;
            color: #2E3F5E;
            padding: 0px;
            p{
                width: 17%;
            }
        }
    }
}
</style>

<style scoped>
/* 运算结果 */

.result-area .flow{
    position: relative;
    height: 100%;
}
.result-area .flow .returnIcon{
    position: absolute;
    top: 13px;
    left: 26px;
    z-index: 99;
    font-size: 20px;
    background-color: #fff;
    cursor: pointer;
}
.result-area .pay{
    height: 100%;
}

.result-box h3{
    font-size: 20px;
    font-weight: 600;
    color: #39486F;
    margin-top: 15px;
    margin-bottom: 30px;
}
.el-table{
    margin-bottom: 15px;
}

.hintp{
    font-size: 13px;
    color: #f87a23;
    line-height: 22px;
    margin-bottom: 60px;
}
.hintp>span{
    display: block;
}
::v-deep .el-table tr td{
    padding: 15px 0;
}
::v-deep .el-table .cell p{
    display: inline-block;
    text-align: center;
}
::v-deep .el-table .cell p.isLight{
    color: #08C160;
    background-color: #ECF9F2;
    border-radius: 30px;
    padding: 0 15px;
}
</style>